package com.ilop.sthome.ui.activity.scene;

import android.util.Log;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.data.event.EventRefreshScene;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.SceneChangeContract;
import com.ilop.sthome.mvp.present.scene.SceneChangePresenter;
import com.ilop.sthome.ui.adapter.scene.SceneChangeAdapter;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityChangeSceneBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：网关中更换场景
 */
public class ChangeSceneActivity extends ContainLoginActivity<SceneChangePresenter, ActivityChangeSceneBinding> implements SceneChangeContract.IView {

    private String mDeviceName;
    private SceneChangeAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_scene;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new SceneChangePresenter(mContext, mDeviceName);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new SceneChangeAdapter(mContext);
        mDBind.rvSceneList.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.getSceneList();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSceneBack.setOnClickListener(view -> finish());
        mDBind.rvSceneList.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresent.getSceneList();
                mDBind.rvSceneList.complete();
            }

            @Override
            public void onLoadMore() {
                mDBind.rvSceneList.complete();
            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.CHANGE_SCENE_LIST, SceneBean.class).observe(this, sceneBean -> {
            if (sceneBean!=null) {
                mPresent.changeScene(sceneBean);
            }
        });
        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_SCENE, EventRefreshScene.class).observe(this, event -> {
            if (event != null){
                if (event.getDeviceName().equals(mDeviceName)) {
                    mPresent.getSceneList();
                }
            }
        });
        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                int result = CoderUtils.getAnswerResult(event.getData_str1(), event.getData_str2());
                Log.i(TAG, "initDataBus: answerOK--" + result);
                if (result == 6){
                    mPresent.saveCurrentState();
                }else if (result == 0){
                    showToast(getString(R.string.failed));
                }
            }
        });
    }

    @Override
    public void showSceneList(List<SceneBean> scene) {
        mAdapter.setList(scene);
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void showProgress() {
        showProgressDialog();
    }

    @Override
    public void setDeleteSceneId(int sceneId) {

    }
}
